import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { h } from 'vue';
import { Image } from 'ant-design-vue';
import { getBasicdataGetOptions } from '/@/api/system/system';
import { useUserStore } from '/@/store/modules/user';
const userStore = useUserStore();
const serverInfo = userStore.getServerInfo;

const typeOptions = [
  { value: '-1', label: '全部' },
  { value: '1', label: '菜单项' },
  { value: '2', label: '流程项' },
  { value: '3', label: '智能表单项' },
];
export const columns: BasicColumn[] = [
  {
    title: '名称',
    dataIndex: 'name',
    width: 150,
    ellipsis: true,
    resizable: true,
  },
  {
    title: '类型',
    dataIndex: 'typeName',
    width: 50,
    ellipsis: true,
    resizable: true,
  },
  {
    title: '图片型图标',
    dataIndex: 'imgUrl',
    width: 80,
    ellipsis: true,
    resizable: true,
    customRender: ({ text }) => {
      const src = text
        ? serverInfo.rootPath + '/static' + (text && text.startsWith('/') ? text : `/${text}`)
        : '';
      if (src) {
        return h(Image, {
          width: 22,
          src,
        });
      }
    },
  },
  {
    title: '上传时间',
    dataIndex: 'setTime',
    width: 100,
    ellipsis: true,
    resizable: true,
  },
  {
    title: '分类',
    dataIndex: 'appTypeName',
    width: 100,
    ellipsis: true,
    resizable: true,
  },
  {
    title: '顺序',
    dataIndex: 'orders',
    width: 100,
    ellipsis: true,
    resizable: true,
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'what',
    label: '名称',
    component: 'Input',
    colProps: { span: 6 },
  },
  {
    field: 'type',
    label: '类型',
    component: 'Select',
    required: true,
    colProps: { span: 6 },
    defaultValue: '-1',
    componentProps: {
      options: typeOptions,
    },
  },
];

export const formSchema: FormSchema[] = [
  {
    field: 'type',
    label: '类型',
    required: true,
    component: 'RadioButtonGroup',
    colProps: { span: 24 },
    defaultValue: 1,
    componentProps: {
      options: [
        { value: 1, label: '菜单项' },
        { value: 2, label: '流程项' },
        { value: 3, label: '模块项' },
        { value: 4, label: '链接项' },
      ],
    },
  },
  {
    field: 'type_menu_selected',
    label: '选择',
    required: true,
    component: 'TreeSelect',
    colProps: { span: 24 },
    slot: 'type_menu_selected',
    ifShow: ({ values }) => values.type == '1',
  },
  {
    field: 'type_flow_selected',
    label: '选择',
    required: true,
    component: 'TreeSelect',
    colProps: { span: 24 },
    slot: 'type_flow_selected',
    ifShow: ({ values }) => values.type == '2',
  },
  {
    field: 'moduleCode',
    label: '选择',
    required: true,
    component: 'Select',
    colProps: { span: 24 },
    slot: 'moduleCode',
    ifShow: ({ values }) => values.type == '3',
  },
  {
    field: 'link',
    label: '链接',
    required: true,
    component: 'Input',
    colProps: { span: 24 },
    ifShow: ({ values }) => values.type == '4',
  },
  {
    field: 'name',
    label: '名称',
    required: true,
    component: 'Input',
    colProps: { span: 24 },
  },
  {
    field: 'imgUrl',
    label: '图片型图标',
    component: 'Select',
    required: true,
    colProps: { span: 24 },
    slot: 'imgUrl',
  },
  {
    field: 'isMobileStart',
    label: '手机端能否快速添加',
    component: 'RadioButtonGroup',
    colProps: { span: 24 },
    required: true,
    defaultValue: 0,
    componentProps: {
      options: [
        { value: 0, label: '否' },
        { value: 1, label: '能' },
      ],
    },
    ifShow: false,
  },
  {
    field: 'appType',
    label: '分类',
    component: 'ApiSelect',
    helpMessage: '基础数据中可维护',
    colProps: { span: 24 },
    defaultValue: '',
    componentProps: {
      api: getBasicdataGetOptions,
      params: { code: 'app_type' },
      showSearch: true,
      labelField: 'name',
      valueField: 'value',
      optionFilterProp: 'label',
      getPopupContainer: () => document.body,
    },
  },
  {
    field: 'orders',
    label: '顺序',
    component: 'InputNumber',
    defaultValue: 0,
    colProps: { span: 24 },
  },
];

export function setChildren(node, bool = true) {
  node.forEach((item) => {
    item.key = item.code;
    item.value = item.code;
    item.title = item.name;
    // if (bool) {
    //   item.disabled = item.layer == 3 ? false : true;
    // }
    if (item.children && Array.isArray(item.children) && item.children.length > 0) {
      item.isLeaf = false;
      setChildren(item.children);
    } else {
      item.isLeaf = true;
    }
  });
  return node;
}
